package com.mdt.doforms.bienen;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMyLocationChangeListener;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.UiSettings;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.mdt.doforms.bienen.mapnlocation.GoogleMapAPIs;
import com.mdt.doforms.bienen.model.DrugStoreLocation;
import com.mdt.doforms.bienen.util.HtdLog;

public class MapActivity extends FragmentActivity implements OnMyLocationChangeListener{
	
	private GoogleMap googleMap;
	private UiSettings mUiSettings;
	private GoogleMapAPIs routePath;
	LatLng currentLocation, drugStoreLoc;
	boolean bFirst = false;
	private int nShowType ;
	DrugStoreLocation drugStore;
	
	ProgressDialog progressDialog;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_map);
		setUpMap();
		
		Bundle bundle = this.getIntent().getExtras();
		if ( null != bundle ){
			nShowType = bundle.getInt(BienenConstants.SHOW_MAP_TYPE_EXTRA_STRING);
			drugStore = (DrugStoreLocation) bundle.get(BienenConstants.SHOW_MAP_DATA_EXTRA_STRING);
			if ( null != drugStore ){
				drugStoreLoc = drugStore.getLocation();
				switch (nShowType) {
				case BienenConstants.SHOW_LOCATION:
					googleMap.addMarker( this.createCustomMarker(drugStore.getName(),drugStore.getAddress(), drugStoreLoc,R.drawable.notdienste_marker));
					moveMapTo(drugStoreLoc, 15);
					break;
				case BienenConstants.SHOW_WITH_ROUTE_PATH:
					googleMap.addMarker( this.createCustomMarker(drugStore.getName(),drugStore.getAddress(), drugStoreLoc,R.drawable.notdienste_marker));
					moveMapTo(drugStoreLoc, 15);
					googleMap.setMyLocationEnabled(true);	
					googleMap.setOnMyLocationChangeListener(this);
//					LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
//					mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 100, this);
//					
					progressDialog = new ProgressDialog(this,ProgressDialog.STYLE_SPINNER);
			        progressDialog.setMessage("Searching current location ...!");
			        progressDialog.setIndeterminate(true);
			        progressDialog.setCanceledOnTouchOutside(false);
			        
			        progressDialog.show();
				        
					break;
				default:
					break;
				}
			}
		}
		
	}
	
	@Override
	protected void onResume() {
	    super.onResume();
	}
	
    private void setUpMap() {
    	// Getting Google Play availability status
        int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());
        
        routePath = new GoogleMapAPIs();
        // Showing status
        if(status!=ConnectionResult.SUCCESS){ // Google Play Services are not available
        	
        	int requestCode = 10;
            Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode);
            dialog.show();
            
        }else {	// Google Play Services are available	
		
			// Getting reference to the SupportMapFragment of activity_main.xml
			SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
			
			// Getting GoogleMap object from the fragment
			googleMap = fm.getMap();
			
			// disabling MyLocation Layer of Google Map
			googleMap.setMyLocationEnabled(false);				
			
			mUiSettings = googleMap.getUiSettings();

	        mUiSettings.setZoomControlsEnabled(true);
	        //Disabling Location Button.
	        mUiSettings.setMyLocationButtonEnabled(false);
	        mUiSettings.setCompassEnabled(true);
	
        }
    }
    
    private MarkerOptions createCustomMarker( String title, String des, LatLng location , int nRes){
    	return new MarkerOptions()
	     .position(location)
	     .title(title)
	     .snippet(des)
	     .icon(BitmapDescriptorFactory.fromResource(nRes));
    }
    
    private void moveMapTo( LatLng location , float fZoom ){
    	// Showing the current location in Google Map
    	googleMap.moveCamera(CameraUpdateFactory.newLatLng(location));
    				
    	// Zoom in the Google Map
    	googleMap.animateCamera(CameraUpdateFactory.zoomTo(fZoom));
    				
    }
    // Location 1: Lat:10.8767845, Long:106.6016855
    // Location 2: Lat:10.8544613, Long:106.6257449

	@Override
	public void onMyLocationChange(Location location) {
		if ( !bFirst ){
			// Getting latitude of the current location
			double latitude = location.getLatitude();
			
			// Getting longitude of the current location
			double longitude = location.getLongitude();		
			
			// Creating a LatLng object for the current location
			currentLocation = new LatLng(latitude, longitude);
			googleMap.addMarker(createCustomMarker("Current Location","", currentLocation,R.drawable.karten_marker));
			// Showing the current location in Google Map
			googleMap.moveCamera(CameraUpdateFactory.newLatLng(currentLocation));
			
			// Zoom in the Google Map
			googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
			progressDialog.dismiss();
			routePath.drawRoute(googleMap, this, currentLocation, drugStoreLoc,  false, "de");
		}
		bFirst = true;
		
	}

//	@Override
//	public void onLocationChanged(Location location) {
//		double latitude = location.getLatitude();
//		
//		// Getting longitude of the current location
//		double longitude = location.getLongitude();		
//		
//		// Creating a LatLng object for the current location
//		currentLocation = new LatLng(latitude, longitude);
//		if ( !bFirst )
//			googleMap.addMarker(createCustomMarker("Current Location","", currentLocation));
//		// Showing the current location in Google Map
//		googleMap.moveCamera(CameraUpdateFactory.newLatLng(currentLocation));
//		
//		// Zoom in the Google Map
//		googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
//		progressDialog.dismiss();
//		routePath.drawRoute(googleMap, this, currentLocation, drugStoreLoc,  true, "de");
//		bFirst = true;
//	}
//
//	@Override
//	public void onProviderDisabled(String arg0) {
//		// TODO Auto-generated method stub
//		
//	}
//
//	@Override
//	public void onProviderEnabled(String arg0) {
//		// TODO Auto-generated method stub
//		
//	}
//
//	@Override
//	public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
//		// TODO Auto-generated method stub
//		
//	}

	
}

